<%-- 
    Document   : Home
    Created on : May 14, 2013, 2:07:54 PM
    Author     : Hue
--%>
<%@page language="java" extends="com.absisgroup.nsc.home.Home"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Nam Thành</title>
        <link rel="stylesheet" type="text/css" href="scripts/resources/css/ext-all.css" />
        <link rel="stylesheet" type="text/css" href="styles/portal.css" />
        <script type="text/javascript" src="scripts/ext-all.js"></script>
        <script type="text/javascript">
            Ext.Loader.setPath('Ext.app', 'scripts/classes');
        </script>
        <script type="text/javascript" src="scripts/home.js?version=<bean:write name="appVersion" ignore="true"/>"></script>
        <script type="text/javascript" src="scripts/Translations.jsp?version=<bean:write name="appVersion" ignore="true"/>"></script>
        <script type="text/javascript" src="scripts/FormatNumberEntity.js"></script>
        <script type="text/javascript">
            Ext.require([
                'Ext.layout.container.*',
                'Ext.resizer.Splitter',
                'Ext.fx.target.Element',
                'Ext.fx.target.Component',
                'Ext.window.Window',
                'Ext.app.ErrorWindow',
                'Ext.app.PhieuBanHangPanel',
                'Ext.app.DanhSachMatHangPanel',
                'Ext.app.DanhSachKhoPanel',
                'Ext.app.NhapKhoWindow',
                'Ext.app.XuatKhoWindow',
                'Ext.app.TaoMoiKhoWindow',
                'Ext.app.CellEditing',
                'Ext.app.XemPhieuBanHangPanel',
                'Ext.app.DanhSachPhieuBanHangPanel',
                'Ext.app.XemPhieuNhapKhoPanel'
            ]);
            
            var formatNumberEntityObj = new FormatNumberEntity("#,###.##", true);
            var formatIntegerEntityObj = new FormatNumberEntity("#,###", false);
            var required = '<span style="color:red;font-weight:bold">*</span>';

            Ext.onReady(function() {
                Ext.QuickTips.init();
                Ext.create('Ext.app.Home', translations, functions);
            });
            
            function formatNumber(n) {
                if(n == null || n == "" || n == undefined) {
                    return n;
                }
                return formatNumberEntityObj.toFormatedNumber(n + "");
            }
            
            function formatInteger(n) {
                if(n == null || n == "" || n == undefined) {
                    return n;
                }
                return formatIntegerEntityObj.toFormatedNumber(n + "");
            }
            
            function displayPanel(panel) {
                var appMainPanel = Ext.getCmp('appMainPanel');
                appMainPanel.removeAll(true);
                panel.region = 'center';
                appMainPanel.add(panel);
            }
            
            function danhSachMatHang() {
                displayPanel({
                    xtype: 'danhSachMatHangPanel',
                    title: translations.get("HOME.DANH_SACH_MAT_HANG_TITLE")
                });
            }
            
            function taoMoiKho() {
                var win = Ext.create('widget.taoMoiKhoWindow', {
                    title: translations.get('HOME.TAO_MOI_KHO')
                });
                win.show();
            }
                
            function nhapKho() {
                var win = Ext.create('widget.nhapKhoWindow', {
                    title: translations.get('HOME.NHAP_KHO')
                });
                win.show();
            }
            
            function xuatKho() {
                var win = Ext.create('widget.xuatKhoWindow', {
                    title: translations.get('HOME.XUAT_KHO')
                });
                win.show();
            }
            
            function showErrorMsg(msg) {
                 if(msg == "NOT_LOGIN") {
                    window.location = "Login.jsp";
                }
                else {
                    var win = Ext.create('widget.errorWindow', {title: "Error"});
                    win.setMsg(msg);
                    win.show();
                }
            }
            
            function lapPhieuBanHang() {
               displayPanel({
                    xtype: 'phieuBanHangPanel',
                    title: translations.get("HOME.PHIEU_BAN_HANG_TITLE")
                });
            }
            
            function danhSachPhieuBanHang() {
                displayPanel({
                    xtype: 'danhSachPhieuBanHangPanel',
                    title: translations.get("HOME.DS_PHIEU_BAN_HANG_TITLE")
                });
            }
            
            function phieuBanHangPDF(button) {
                var orderPK = button.up('xemPhieuBanHangPanel').orderPK;
                printPDF(orderPK);
            }
            
            function printPDF(orderPK) {
                var submitForm = document.phieuBanHangPDFForm;
                submitForm.orderPK.value = orderPK;
                submitForm.submit();
            }
            
            Ext.renderPopupIcon = function() {
                return '<img src="images/popupmenu.gif" style="cursor:pointer" height="13">';
            }
            
            function openPopupMenu(e, items) {
                var contextMenu = Ext.getCmp("contextMenu");
                if (contextMenu != undefined) {
                    contextMenu.removeAll(true);
                    contextMenu.close();
                }
                contextMenu = Ext.create('Ext.menu.Menu', {
                    id: "contextMenu",
                    items: items
                });

                var addMenuMouseOver = function(menu) {
                    var hideTasks = new Ext.util.DelayedTask(menu.hide, menu); 
                    menu.on({ 
                        mouseover: function() { 
                            hideTasks.cancel(); 
                        },
                        mouseout: function() { 
                            hideTasks.delay(250); 
                        }
                    }); 

                    menu.items.each(function(item){ 
                        if (item.menu) { 
                            addMenuMouseOver(item.menu); 
                        } 
                    }); 
                }
                addMenuMouseOver(Ext.getCmp("contextMenu"));
                contextMenu.showAt(e.getXY());
            }
            
            function xemPhieuBanHang(orderPK) {
                Ext.getBody().mask(translations.get("HOME.PLEASE_WAIT"));
                Ext.Ajax.request({
                    url: 'XemPhieuBanHang.jsp',
                    params: {
                        'orderPK': orderPK
                    },
                    method: 'POST',
                    success: function(response, opts) {
                        Ext.getBody().unmask();
                        var obj = Ext.decode(response.responseText);
                        if(!obj.success) {
                            showErrorMsg(obj.msg);
                        }
                        else {
                            displayPanel({
                                xtype: 'xemPhieuBanHangPanel',
                                title: translations.get("HOME.PHIEU_BAN_HANG_TITLE") + " - " + obj.orderNo + " - " + obj.dateText
                            });
                            Ext.getCmp("xemPhieuBanHangPanel").setRecords(obj);
                        }
                    },
                    failure: function(response, opts) {
                        Ext.getBody().unmask();
                        alert('server-side failure with status code ' + response.status);
                    }
                });
            }
            
            function xemPhieuNhapKhoPanel(impProdPK) {
                Ext.getBody().mask(translations.get("HOME.PLEASE_WAIT"));
                Ext.Ajax.request({
                    url: 'XemPhieuNhapKho.jsp',
                    params: {
                        'impProdPK': impProdPK
                    },
                    method: 'POST',
                    success: function(response, opts) {
                        Ext.getBody().unmask();
                        var obj = Ext.decode(response.responseText);
                        if(!obj.success) {
                            showErrorMsg(obj.msg);
                        }
                        else {
                            displayPanel({
                                xtype: 'xemPhieuNhapKhoPanel',
                                title: translations.get("HOME.PHIEU_NHAP_KHO_TITLE") + " - " + obj.impProdNo + " - " + obj.dateText
                            });
                            getPanel("xemPhieuNhapKhoPanel").setData(obj);
                        }
                    },
                    failure: function(response, opts) {
                        Ext.getBody().unmask();
                        alert('server-side failure with status code ' + response.status);
                    }
                });
            }
            
            function getPanel(type) {
                var appMainPanel = Ext.getCmp("appMainPanel");
                return appMainPanel.down(type);
            }
        </script>
    </head>
    <body>
        <span style="display:none">
            <iframe name="hiddenFrame"></iframe>
            <form name="phieuBanHangPDFForm" method="post" action="PhieuBanHangPDF.jsp" target="hiddenFrame">
                <input type="hidden" name="orderPK">
            </form>
        </span>
    </body>
</html>
